<template>
	<view>
		<s-layout :onShareAppMessage="shareInfo" navbar="goods">
			<!-- 标题栏 -->
			<detailNavbar />

			<!-- 骨架屏 -->
			<detailSkeleton v-if="state.skeletonLoading" />
			<!-- 下架/售罄提醒 -->
			<s-empty v-else-if="state.goodsInfo === null" text="商品不存在或已下架" icon="/static/soldout-empty.png" showAction
				actionText="再逛逛" actionUrl="/pages/goods/list" />
			<block v-else>
				<view class="detail-swiper-selector">
					<!-- 商品轮播图  -->
					<su-swiper class="ss-m-b-14" isPreview :list="state.goodsSwiper" dotStyle="tag" imageMode="aspectFill"
						dotCur="bg-mask-40" :seizeHeight="650" />

					<!-- 价格+标题 -->
					<view class="title-card detail-card ss-p-y-40 ss-p-x-20">
						<view class="ss-flex ss-row-between ss-col-center ss-m-b-26">
							<view class="price-box ss-flex ss-col-bottom">
								<view class="price-text ss-m-r-16 font-ali-bold">
									{{ state.selectedSkuPrice.price || formatPrice(state.goodsInfo.price) }}
								</view>
								<view class="origin-price-text font-ali-bold" v-if="state.goodsInfo.original_price > 0">
									{{ state.selectedSkuPrice.original_price || state.goodsInfo.original_price }}
								</view>
							</view>
							<view class="sales-text">
								{{ formatSales(state.goodsInfo.sales_show_type, state.goodsInfo.sales) }}
							</view>
						</view>
						<!--            <view class="discounts-box ss-flex ss-row-between ss-m-b-28">-->
						<!--              <div class="tag-content">-->
						<!--                <view class="tag-box ss-flex">-->
						<!--                  <view-->
						<!--                    class="tag ss-m-r-10"-->
						<!--                    v-for="promos in state.goodsInfo.promos"-->
						<!--                    :key="promos.id"-->
						<!--                    @tap="onActivity"-->
						<!--                  >-->
						<!--                    {{ promos.title }}-->
						<!--                  </view>-->
						<!--                </view>-->
						<!--              </div>-->

						<!--              <view-->
						<!--                class="get-coupon-box ss-flex ss-col-center ss-m-l-20"-->
						<!--                @tap="state.showModel = true"-->
						<!--                v-if="state.couponInfo.length"-->
						<!--              >-->
						<!--                <view class="discounts-title ss-m-r-8">领券</view>-->
						<!--                <text class="cicon-forward"></text>-->
						<!--              </view>-->
						<!--            </view>-->
						<view class="title-text ss-line-2 ss-margin-left-12 ss-m-b-6">
							<text class="title-tag" :style="{backgroundColor:state.type.color}">{{ state.type.name }}</text>
							<text>{{ state.goodsInfo.title }}</text>
						</view>
						<view class="subtitle-text ss-line-1">{{ state.goodsInfo.subtitle }}</view>
					</view>

					<!-- 功能卡片 -->
					<view class="detail-cell-card detail-card ss-flex-col">
						<detail-cell-sku v-model="state.selectedSkuPrice.goods_sku_text" :skus="state.goodsInfo.skus"
							@tap="state.showSelectSku = true" />
						<detail-cell-status v-if="state.goodsInfo.status" v-model="state.goodsInfo.status" />
						<view style="display:flex;padding: 10rpx 20rpx;">
							<view style="color: font-size: 28rpx;font-weight: 500;margin-right: 38rpx;color:#999">物流
							</view>
							<view style="font-size: 28rpx;font-weight: 500;color: #666;">{{state.goodsInfo.single_post == 1 ? '单品包邮' : '满88包邮'}}</view>
						</view>
						<!-- <detail-cell-service v-if="state.goodsInfo.service" v-model="state.goodsInfo.service" /> -->
						<detail-cell-params v-if="state.goodsInfo.params" v-model="state.goodsInfo.params" />
					</view>

					<!-- 规格与数量弹框 -->
					<s-select-sku :goodsInfo="state.goodsInfo" :show="state.showSelectSku" @addCart="onAddCart" @buy="onBuy"
						@change="onSkuChange" @close="state.showSelectSku = false" />
				</view>

				<!-- 评价 2024-12-27客户隐藏-->
				<!-- <detail-comment-card class="detail-comment-selector" :goodsId="state.goodsId" /> -->
				<!-- 详情 -->
				<view v-if="state.contentInfoType == 'phone'" class="detail-content-card bg-white ss-m-x-20 ss-p-t-20">
					<view class="card-header ss-flex ss-col-center ss-m-b-30 ss-m-l-20">
						<view class="line"></view>
						<view class="title ss-m-l-20 ss-m-r-20">详情</view>
					</view>
					<view class="card-content" v-if="state.goodsInfo.content.length > 0">
						<view class="form-user">
							<view class="special-item">
								<view class="special-item-panel-list" v-for="(item, index) in state.goodsInfo.content"
									:key="index">
									<view class="special-item-panel-list-image" v-if="item.type == 'img'">
										<image :src="item.content" style="width: 100%;" mode="widthFix"></image>
									</view>
									<view class="special-item-panel-list-text" v-if="item.type == 'text'">
										<rich-text :nodes="item.content"></rich-text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<!-- PC端添加的商品详情展示格式为富文本标签 -->
				<view v-if="state.contentInfoType == 'PC'" class="detail-content-card bg-white ss-m-x-20 ss-p-t-20">
					<view class="card-header ss-flex ss-col-center ss-m-b-30 ss-m-l-20">
						<view class="line"></view>
						<view class="title ss-m-l-20 ss-m-r-20">详情.</view>
					</view>
					<view class="card-content" style="width: 100%;" v-if="state.goodsInfo.content != ''">
						<rich-text style="width: 100%;" :nodes="state.goodsInfo.content"></rich-text>
						<!-- <view style="width: 100%;" v-html="state.goodsInfo.content"></view> -->
					</view>
				</view>
				<!-- <detail-content-card class="detail-content-selector" :content="state.goodsInfo.content" /> -->

				<!-- 活动跳转 -->
				<detail-activity-tip v-if="state.goodsInfo.activities" :data="state.goodsInfo"></detail-activity-tip>

				<!-- 详情tabbar -->
				<detail-tabbar v-model="state.goodsInfo">
					<!-- TODO: 缺货中 已售罄 判断 设计-->
					<view class="buy-box ss-flex ss-col-center ss-p-r-20" v-if="state.goodsInfo.stock > 0">
						<button style="background: #2d2d2d;color: #fff;border-radius:12rpx 0 0 12rpx;font-weight: normal;"
							class="ss-reset-button add-btn" @tap="state.showSelectSku = true">
							加入购物车
						</button>
						<button style="background: #ff3642;color: #fff;border-radius:0 12rpx 12rpx 0;font-weight: normal;"
							class="ss-reset-button buy-btn" @tap="state.showSelectSku = true">
							立即购买
						</button>
					</view>
					<view class="buy-box ss-flex ss-col-center ss-p-r-20" v-else>
						<button class="ss-reset-button disabled-btn" disabled> 已售罄 </button>
					</view>
				</detail-tabbar>
				<s-coupon-get v-model="state.couponInfo" :show="state.showModel" @close="state.showModel = false"
					@get="onGet" />
				<s-activity-pop v-model="state.activityInfo" :show="state.showActivityModel"
					@close="state.showActivityModel = false" />
			</block>
		</s-layout>
	</view>
</template>

<script setup>
import {
	reactive,
	computed
} from 'vue';
import {
	onLoad,
	onPageScroll
} from '@dcloudio/uni-app';
import sheep from '@/sheep';
import {
	formatSales,
	formatGoodsSwiper,
	formatPrice
} from '@/sheep/hooks/useGoods';
import detailNavbar from './components/detail/detail-navbar.vue';
import detailCellSku from './components/detail/detail-cell-sku.vue';
import detailCellService from './components/detail/detail-cell-service.vue';
import detailCellParams from './components/detail/detail-cell-params.vue';
import detailCellStatus from './components/detail/detail-cell-status.vue';
import detailTabbar from './components/detail/detail-tabbar.vue';
import detailSkeleton from './components/detail/detail-skeleton.vue';
import detailCommentCard from './components/detail/detail-comment-card.vue';
import detailContentCard from './components/detail/detail-content-card.vue';
import detailActivityTip from './components/detail/detail-activity-tip.vue';
import {
	isEmpty
} from 'lodash';

// import detailActivityTip from './components/detail/detail-activity-tip.vue';
// import detailTab from './components/detail/detail-tab.vue';
// import detailCoupon from './components/detail/detail-coupon.vue';
onPageScroll(() => {});

const goodsType = computed(() => sheep.$store('goods').type);

const state = reactive({
	goodsId: 0,
	skeletonLoading: true,
	goodsInfo: {},
	showSelectSku: false,
	goodsSwiper: [],
	selectedSkuPrice: {},
	showModel: false,
	total: 0,
	couponInfo: [],
	showActivityModel: false,
	activityInfo: [],
	type: {},
	contentInfoType:'PC'
});

function getGoodsType (id) {
	const type = sheep.$store('goods').type;
	return type.find((item) => item.id == id);
}

// 规格变更
function onSkuChange (e) {
	state.selectedSkuPrice = e;
}

// 添加购物车
function onAddCart (e) {
	sheep.$store('cart').add(e);
}

// 立即购买
function onBuy (e) {
	sheep.$router.go('/pages/order/confirm', {
		data: JSON.stringify({
			order_type: 'goods',
			goods_list: [{
				goods_id: e.goods_id,
				goods_num: e.goods_num,
				goods_sku_price_id: e.id,
			}, ],
		}),
	});
}
//营销活动
function onActivity () {
	state.activityInfo = state.goodsInfo.promos;
	state.showActivityModel = true;
}

//立即领取
async function onGet (id) {
	const {
		code,
		msg
	} = await sheep.$api.coupon.get(id);
	if (code === 1) {
		uni.showToast({
			title: msg,
		});
		setTimeout(() => {
			getCoupon();
		}, 1000);
	}
}

const shareInfo = computed(() => {
	if (isEmpty(state.goodsInfo)) return {};
	return sheep.$platform.share.getShareInfo({
		title: state.goodsInfo.title,
		image: sheep.$url.cdn(state.goodsInfo.image),
		desc: state.goodsInfo.subtitle,
		params: {
			page: '2',
			query: state.goodsInfo.id,
		},
	}, {
		type: 'goods', // 商品海报
		title: state.goodsInfo.title, // 商品标题
		image: sheep.$url.cdn(state.goodsInfo.image), // 商品主图
		price: state.goodsInfo.price[0], // 商品价格
		original_price: state.goodsInfo.original_price, // 商品原价
	}, );
});

function isValidJSON(text) {
  try {
    JSON.parse(text);
    return true;
  } catch (error) {
    return false;
  }
}
onLoad(async (options) => {
	// 非法参数
	if (!options.id) {
		state.goodsInfo = null;
		return;
	}
	state.goodsId = options.id;
	// 加载商品信息
	sheep.$api.goods.detail(state.goodsId).then((res) => {
		state.skeletonLoading = false;
		if (res.code === 1) {
			state.goodsInfo = res.data;
			console.log(isValidJSON(state.goodsInfo.content),'是否是json字符串')
			if(isValidJSON(state.goodsInfo.content)){
				state.contentInfoType = 'phone'
				state.goodsInfo.content = JSON.parse(state.goodsInfo.content)
			}else{
				state.goodsInfo.content = state.goodsInfo.content.replace(/style=""/g,'')
				state.goodsInfo.content = state.goodsInfo.content.replace(/\<img/g,'<img class="rich-text-class" style="width:100%;max-width:100%;height:auto"')
				state.contentInfoType = 'PC'
			}
			console.log(state.goodsInfo.content, 'state.goodsInfo ++')
			state.goodsSwiper = formatGoodsSwiper(state.goodsInfo.images);
			state.type = getGoodsType(state.goodsInfo.isxh);
			state.goodsInfo.status = [{
				title: state.type.name,
				content: state.type.name + '商品'
			}];
		} else {
			// 未找到商品
			state.goodsInfo = null;
		}
	});
	const {
		code,
		data
	} = await sheep.$api.coupon.listByGoods(state.goodsId);
	if (code === 1) {
		state.couponInfo = data;
	}
});
</script>

<style lang="scss" scoped>
.rich-text-class{
	width: 100%;
	max-width: 100%;
	overflow: hidden;
}
	img {
		max-width:100%;
	}
.detail-card {
	background-color: #ffff;
	margin: 14rpx 20rpx;
	border-radius: 10rpx;
	overflow: hidden;
}

// 价格标题卡片
.title-card {
	.price-box {
		.price-text {
			font-size: 42rpx;
			font-weight: 500;
			color: #ee3f39;
			;
			line-height: 30rpx;

			&::before {
				content: '￥';
				font-size: 30rpx;
			}
		}

		.origin-price-text {
			font-size: 26rpx;
			font-weight: 400;
			text-decoration: line-through;
			color: $gray-c;

			&::before {
				content: '￥';
			}
		}
	}

	.sales-text {
		font-size: 26rpx;
		font-weight: 500;
		color: $gray-c;
	}

	.discounts-box {
		.tag-content {
			flex: 1;
			min-width: 0;
			white-space: nowrap;
		}

		.tag-box {
			overflow: hidden;
			text-overflow: ellipsis;
		}

		.tag {
			flex-shrink: 0;
			padding: 4rpx 10rpx;
			font-size: 24rpx;
			font-weight: 500;
			border-radius: 4rpx;
			color: var(--ui-BG-Main);
			background: var(--ui-BG-Main-tag);
		}

		.discounts-title {
			font-size: 24rpx;
			font-weight: 500;
			color: var(--ui-BG-Main);
			line-height: normal;
		}

		.cicon-forward {
			color: var(--ui-BG-Main);
			font-size: 24rpx;
			line-height: normal;
			margin-top: 4rpx;
		}
	}

	.title-text {
		font-size: 33rpx;
		font-weight: bold;
		line-height: 42rpx;

		.title-tag {
			background: #5acd30;
			padding: 2rpx 4rpx;
			font-size: 20rpx;
			color: #fff;
			border-radius: 10rpx;
			vertical-align: middle;
			margin-right: 6rpx;
		}

		>text {
			vertical-align: middle;
		}
	}

	.subtitle-text {
		font-size: 26rpx;
		font-weight: 400;
		color: $dark-9;
		line-height: 42rpx;
	}
}

// 购买
.buy-box {
	.add-btn {
		width: 214rpx;
		height: 72rpx;
		font-weight: 500;
		font-size: 28rpx;
		border-radius: 40rpx 0 0 40rpx;
		background-color: var(--ui-BG-Main-light);
		color: var(--ui-BG-Main);
	}

	.buy-btn {
		width: 214rpx;
		height: 72rpx;
		font-weight: 500;
		font-size: 28rpx;

		border-radius: 0 40rpx 40rpx 0;
		background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
		color: $white;
	}

	.disabled-btn {
		width: 428rpx;
		height: 72rpx;
		font-weight: 500;
		font-size: 28rpx;

		border-radius: 0 40rpx 40rpx 0;
		background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
		color: $white;
	}
}

.model-box {
	height: 60vh;

	.model-content {
		height: 56vh;
	}

	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}

	.subtitle {
		font-size: 26rpx;
		font-weight: 500;
		color: #333333;
	}
}

// s商品详情央视
.detail-content-card {
	.card-header {
		.line {
			width: 6rpx;
			height: 30rpx;
			background: linear-gradient(180deg, var(--ui-BG-Main) 0%, var(--ui-BG-Main-gradient) 100%);
			border-radius: 3rpx;
		}

		.title {
			font-size: 30rpx;
			font-weight: bold;
		}

		.des {
			font-size: 24rpx;
			color: $dark-9;
		}

		.more-btn {
			font-size: 24rpx;
			color: var(--ui-BG-Main);
		}
	}
}


.form-user {
	border-bottom: 20rpx solid #f3f4f8;

	.title {
		font-family: Source Han Sans CN-Medium, Source Han Sans CN;
		font-weight: 600;
		color: #252e3b;
		font-size: 32rpx;
		box-sizing: border-box;
		padding: 30rpx 32rpx 20rpx 32rpx;
	}

	.form-item {
		display: flex;
		align-items: center;
		height: 102rpx;
		padding: 0 32rpx;
		border-bottom: 1px solid #f3f4f8;

		.form-item-panel {
			display: flex;
			align-items: center;
			word-break: break-all;
			width: 25%;

			:nth-child(1) {
				color: #656878;
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				padding-left: 5rpx;
			}

			:nth-child(2) {
				color: #ee2b1e;
			}
		}

		.form-item-content {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.form-item-content-input {
				color: #252e3b;
				font-size: 28rpx;
				flex: 1;

				.input-placeholder {
					color: rgba(0, 0, 0, 0.3);
					font-size: 28rpx;
				}
			}

			.form-item-content-upload {
				.upload {
					width: 70rpx;
					height: 70rpx;
					background: rgba(238, 43, 30, 0.08);
					border-radius: 16rpx;
					display: flex;
					align-items: center;
					justify-content: center;
				}

				image {
					width: 70%;
					height: 70%;
				}
			}

			.form-item-content-more {
				width: 30rpx;
				height: 35rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}

	.special-item {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		padding: 0 32rpx;
		border-bottom: 1px solid #f3f4f8;
		padding-bottom: 32rpx;

		.special-item-panel-list {
			display: flex;
			flex-direction: column;
			align-items: center;
			width: 100%;

			.special-item-panel-list-video {
				width: 100%;

				image {
					padding: 10rpx 0;
					width: 100%;
					height: 350rpx;
				}
			}

			.special-item-panel-list-image {
				width: 100%;

				video {
					padding: 10rpx 0;
					width: 96%;
					// height: 350rpx;
				}
			}

			.special-item-panel-list-text {
				color: #656878;
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				padding: 20rpx;
				box-sizing: border-box;
				letter-spacing: 2rpx;
				color: #252e3b;
				text-align: left;
				text-indent: 2em;
				width: 100%;
			}
		}

	}
}
</style>